home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / asm / misc / printftext / asm_printf_test.s
Text File  |  1980-01-03  |  4KB  |  133 lines

  1.  
  2.         section    test_printf,code    ;use publicmem
  3.  
  4. ProgStart    bsr.b    _PrintInit        ;initialise printf
  5.         beq.s    Quit            ;failed?
  6.  
  7.         lea    hello(pc),a0        ;format string
  8.         lea    hello.arg(pc),a1    ;arguments string
  9.         bsr.b    printf
  10.  
  11.         bsr.b    _PrintClose        ;free printf memory
  12. Quit        rts
  13.  
  14. hello        dc.b    "Hello, %ld World!",10,0
  15.         even
  16. hello.arg    dc.l    911
  17.         even
  18.  
  19. AbsExecBase    =    $4
  20. MEMF_CLEAR    =    $10000
  21. MEMF_PUBLIC    =    $1
  22.  
  23. _LVOFreeMem    =    -$D2
  24. _LVORawPutChar    =    -$204
  25. _LVORawDoFmt    =    -$20A
  26. _LVOOutput    =    -$3C
  27. _LVOWrite    =    -$30
  28. _LVOOldOpenLibrary    =    -$198
  29. _LVOAllocMem    =    -$C6
  30. _LVORawIOInit    =    -$1F8
  31.  
  32. *****************************************************************************
  33. * PrintInit( )
  34. * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  35. * $Inputs:    None.
  36. * $Outputs:    d0.l=0 if failed.. 
  37. *
  38. * Initializes further usage of Print( ) function.
  39. * Gets output handle, calls AllocMem() for 127 byte output buffer, and
  40. * RawIOInit().  Preserves all registers used.  This should be placed
  41. * in your "opening" routine before any printf() calls.
  42. * D0 will contain a non-zero value ( Z = 0 ) if AllocMem() was successful,
  43. * otherwise D0 = 0 ( Z = 1 ) on return if failure.
  44. *
  45. * ( If PrintInit() fails, you don't need to quit.  Print( ) and can tell that 
  46. * the allocation failed and will return without doing anything. )
  47. ******************************************************************************
  48.  
  49. _PrintInit:    movem.l    d0/d1/a0/a1/a4/a6,-(sp)
  50.         lea    _DOSBase(pc),a4
  51.         move.l    (AbsExecBase).w,a6
  52.         lea    DosName(pc),a1
  53.         jsr    _LVOOldOpenLibrary(a6)        ;open dos library
  54.         move.l    d0,(a4)
  55.         beq.s    _NoPrintInit
  56.         move.l    d0,a6
  57.         jsr    _LVOOutput(a6)            ;get output base
  58.         move.l    d0,_DosOutput-_DOSBase(a4)
  59.  
  60.         move.l    (AbsExecBase).w,a6
  61.         moveq    #127,d0
  62.         move.l    #(MEMF_PUBLIC+MEMF_CLEAR),d1
  63.         jsr    _LVOAllocMem(a6)        ;allocate 127 bytes
  64.         move.l    d0,_MemPtr1-_DOSBase(a4)
  65.         jsr    _LVORawIOInit(a6)        ;do a raw IO init
  66. _NoPrintInit:    movem.l    (sp)+,d0/d1/a0/a1/a4/a6
  67.         rts
  68.  
  69. ******************************************************************************
  70. * PrintClose( )
  71. * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  72. *
  73. * If AllocMem() from PrintInit( ) was successful, this does a FreeMem()
  74. * freeing the 127 byte output buffer.  Preserves all registers used.
  75. * This should be placed in your "closing" routine after last printf() call.
  76. ******************************************************************************
  77.  
  78. _PrintClose:    movem.l    d0/d1/a0/a1/a6,-(sp)
  79.         lea    _MemPtr1(pc),a1
  80.         tst.l    (a1)
  81.         beq.s    _NoPrintClose
  82.         move.l    (a1),a1
  83.         moveq    #127,d0
  84.         move.l    (AbsExecBase).w,a6
  85.         jsr    _LVOFreeMem(a6)            ;free raw io memory
  86.  
  87. _NoPrintClose:    movem.l    (sp)+,d0/d1/a0/a1/a6
  88.         rts
  89.  
  90. ******************************************************************************
  91. * Usage:  printf( FormatString DataStream )
  92. *                   A0            A1
  93. *
  94. * Performs "C" language like formatting of the data stream. Where % formatting
  95. * cmd`s are found in FormatString, they're replaced with the corresponding 
  96. * element in the DataStream.
  97. *
  98. * Printf() outputs the results to the current output handle, usually the CLI.
  99. ********************************************************************************
  100.  
  101. printf:        movem.l    d0-d3/a0-a3/a6,-(sp)
  102.         lea    PrintChar(pc),a2
  103.         tst.l    _MemPtr1-PrintChar(a2)
  104.         beq.s    _NoPrint
  105.  
  106.         move.l    (AbsExecBase).w,a6
  107.         move.l    _MemPtr1(pc),a3
  108.         jsr    _LVORawDoFmt(a6)
  109.  
  110.         move.l    _DOSBase(pc),a6
  111.         move.l    _DosOutput(pc),d1
  112.         move.l    _MemPtr1(pc),d2
  113.  
  114.         move.l    _MemPtr1(pc),a3
  115.         moveq    #-1,d3
  116. getlen        addq.l    #1,d3
  117.         tst.b    (a3)+
  118.         bne.s    getlen
  119.         jsr    _LVOWrite(a6)
  120.  
  121. _NoPrint:    movem.l    (sp)+,d0-d3/a0-a3/a6
  122.         rts
  123. PrintChar:    move.b    d0,(a3)+
  124.         rts
  125.  
  126. DosName:    dc.b    'dos.library',0
  127.         even
  128. _DOSBase:    dc.l    0
  129. _MemPtr1:    dc.l    0
  130. _DosOutput:    dc.l    0
  131.  
  132.         end
  133.